#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent),
      currentGender("")
{
    this->resize(200,200);
    OKbtn = new QPushButton("OK");
    toolbtn = new QToolButton;
    toolbtn->setText("工");
    manbtn = new QRadioButton("男",this);
    manbtn->setCheckable(true);
    femanbtn = new QRadioButton("女",this);

    genderGroup = new QButtonGroup(this);
    genderGroup->addButton(manbtn,0);
    genderGroup->addButton(femanbtn,1);

    QVBoxLayout* vbox= new QVBoxLayout;
    vbox->addWidget(toolbtn);
    vbox->addWidget(manbtn);
    vbox->addWidget(femanbtn);
    vbox->addWidget(OKbtn);
    setLayout(vbox);
    //连接性别选择信号到槽
    connect(genderGroup,QOverload<int>::of(&QButtonGroup::buttonClicked),
                                           this,&Widget::onGenderSelected);
    //连接自定义信号到槽函数
    connect(this,&Widget::genderSelected,this,&Widget::printGender);
    connect(OKbtn,&QPushButton::clicked,this,&Widget::showResultWindow);
}
Widget::~Widget()
{
}

void Widget::onGenderSelected(int id)
{
    currentGender = (id==0) ? "男" : "女";
    emit genderSelected(currentGender);
}

void Widget::printGender(const QString &gender)
{
    qDebug()<<"现在选的性别是"<<gender;
}

void Widget::showResultWindow()
{
    if(currentGender.isEmpty())
    {
        QMessageBox::warning(this,"提示","请先输入性别");
        return;
    }
    QWidget *result =new QWidget();
    result ->setWindowTitle("用户性别");
    result->resize(500,500);

    QLabel *label =new QLabel("选择的性别是："+currentGender,result);
    label ->setAlignment(Qt::AlignHCenter);

    QVBoxLayout *layout = new QVBoxLayout(result);
    layout->addWidget(label);

    result->setAttribute(Qt::WA_DeleteOnClose);
    result->show();
}


